package 蓝桥算法;

public class day03 {
    public int[][] generateMatrix(int n){//创建螺旋矩阵
        int [][]nums=new int[n][n];
        int startx=0;
        int starty=0;
        int count=0;
        int offect=1;
        int loop=1;
        int i,j;
        while(loop<=(n/2)){
            for(j=starty;j<n-offect;j++){
                nums[startx][j]=count++;
            }
            for (i = startx; i < n-offect; i++) {
                  nums[i][j]=count++;
            }
            for (; j>starty;j--) {
                nums[i][j]=count++;
            }
            for (;i>startx;i--){
                nums[i][j]=count++;
            }
            startx++;
            starty++;
            loop++;
            offect++;
            if(n%2==1){//n为奇数时，单独处理中心矩阵
                nums[startx][starty]=count;
            }
        }
        return nums;
    }
    //链表指针的移除在链表包
    public static void main(String[] args) {
        day03 a=new day03();
        int [][] nums=a.generateMatrix(3);
        for (int i = 0; i <nums.length ; i++) {
            for (int j = 0; j < nums.length; j++) {
                System.out.print(nums[i][j]);
            }
            System.out.println();
        }
    }
}


